package com.mynetwork.activity;

import java.util.ArrayList;


import com.mynetwork.R;
import com.mynetwork.R.id;
import com.mynetwork.data.ContentInfo;
import com.mynetwork.data.ContentManager;
import com.mynetwork.data.NetworkManager;
import com.mynetwork.data.OnNetworkListener;
import com.mynetwork.utils.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class listFragment extends Fragment {

	private String tag = "listFragment";

	public ArrayList<ContentInfo> mInfos;
	
	private ImageView wanted_img;
	
	private TextView wanted_title;
	private TextView wanted_address;
	private ListView list_wanted;

	private imageAdapter mimageAdapter;
	
	private ProgressBar img_ring;
	
	Button more;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.listfragment, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		mInfos = ContentManager.getInstance().getContentList();
		list_wanted = (ListView) getView().findViewById(R.id.list_wanted);
		list_wanted.setDivider(null);
		init();
		
	}

	private void init() {
		
		
		more =  new Button(getActivity());
		more.setText("더보기");
		list_wanted.addFooterView(more);
		mimageAdapter = new imageAdapter(getActivity(), mInfos, getActivity());
		list_wanted.setAdapter(mimageAdapter);
		
		list_wanted.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}
		});
		
		list_wanted.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				if ((mInfos.size() >= NetworkManager.listCount+20) ) {
					more.setVisibility(View.VISIBLE);
				}else {
					more.setVisibility(View.GONE);
				}
//				Log.e("scrollState", "" +scrollState);
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
//				Log.e("firstVisibleItem", "" + firstVisibleItem);
//				Log.e("visibleItemCount", "" + visibleItemCount);
//				Log.e("totalItemCount", "" + totalItemCount);
				if (firstVisibleItem%20 == 0  && firstVisibleItem!=0 && firstVisibleItem != 1) {
					mimageAdapter.imageLoader.clearCache();
				}
				Log.e("산수", "" + firstVisibleItem%20);
			}
		});
		final int currentPosition = list_wanted.getFirstVisiblePosition();
		more.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ((mInfos.size() >= NetworkManager.listCount+20) ) {
					NetworkManager.listCount += 20;
					ContentManager.getInstance().download(getActivity(), null);
					mimageAdapter.notifyDataSetChanged();
					list_wanted.setSelectionFromTop(currentPosition +15, 0);
					more.setVisibility(View.VISIBLE);
				}else {
					more.setVisibility(View.GONE);
				}
				mimageAdapter.imageLoader.clearCache();
			}
		});
	}
	
//	private BitmapDownloadManager mBmpManager = new BitmapDownloadManager();

	public class imageAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<ContentInfo> items;
		private LayoutInflater inflater;
		public ImageLoader imageLoader; 
		
		public imageAdapter(Context context, ArrayList<ContentInfo> items,
				Activity act) {
			this.context = context;
			this.items = items;
			this.inflater = LayoutInflater.from(context);
			imageLoader=new ImageLoader(context.getApplicationContext());
			
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ContentInfo item = getItem(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.listitem, null);
			}
			wanted_title = (TextView) convertView.findViewById(R.id.wanted_title);
			wanted_title.setText(item.title);
			
			wanted_address = (TextView) convertView.findViewById(R.id.wanted_address);
			wanted_address.setText(item.address);
			
			
			wanted_img = (ImageView) convertView.findViewById(R.id.wanted_img);
			
			 imageLoader.DisplayImage(item.imgUrls.get(0), wanted_img, new com.mynetwork.utils.ImageLoader.OnDownloadListener() {
				
				@Override
				public void ondownloadfinish() {
					// TODO Auto-generated method stub
				}
			});
			return convertView;
		 }

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public ContentInfo getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
	}
}
